package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 消费者监听器类
 */
@Component
public class ConsumerListener {

    /**
     * 简单工作模型-消费者-监听消息的方法
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void simpleListener(String msg){
        System.out.println("简单工作模型-消费者-监听消息:"+msg);
    }

    /**
     * 工作队列模型-消费者1-监听消息的方法
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workQueueListener1(String msg) throws InterruptedException {
        System.out.println("工作队列模型-消费者1-监听消息："+msg);
        TimeUnit.MILLISECONDS.sleep(10);//睡眠10毫秒-用来模拟消费者1-处理消息耗时10毫秒
    }
    /**
     * 工作队列模型-消费者2-监听消息的方法
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workQueueListener2(String msg) throws InterruptedException {
        System.out.println("工作队列模型-消费者2-监听消息："+msg);
        TimeUnit.MILLISECONDS.sleep(100);//睡眠100毫秒-用来模拟消费者1-处理消息耗时100毫秒
    }

    /**
     * 广播模型-消费者1-监听消息的方法
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutListener1(String msg){
        System.out.println("广播模型-消费者1-监听消息："+msg);
    }

    /**
     * 广播模型-消费者2-监听消息的方法
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void fanoutListener2(String msg){
        System.out.println("广播模型-消费者2-监听消息："+msg);
    }

    /**
     * 路由模型-消费者1-监听消息的方法
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void directListener1(String msg){
        System.out.println("路由模型-消费者1-监听消息"+msg);
    }
    /**
     * 路由模型-消费者2-监听消息的方法
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void directListener2(String msg){
        System.out.println("路由模型-消费者2-监听消息"+msg);
    }
    /**
     * 路由模型-消费者3-监听消息的方法
     */
    @RabbitListener(queues = "direct.queue3")
    public void directListener3(String msg){
        System.out.println("路由模型-消费者3-监听消息"+msg);
    }

    /**
     * 主题模型-消费者1-监听消息的方法
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void topicListener1(String msg){
        System.out.println("主题模型-消费者1-监听消息"+msg);
    }


    /**
     * 主题模型-消费者2-监听消息的方法
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),
            key = "*.news"
    ))
    public void topicListener2(String msg){
        System.out.println("主题模型-消费者2-监听消息"+msg);
    }
}
